//
//  TypeScrollView.m
//  Bread-Live
//
//  Created by ios on 2017/3/13.
//  Copyright © 2017年 Chris. All rights reserved.
//

#import "TypeScrollView.h"

#define BTNTag  200
@interface TypeScrollView ()

@property(nonatomic,strong) UIButton *typeBtn;
@property(nonatomic,strong) UIButton *oldBtn;

@end

@implementation TypeScrollView


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.bounces = NO;
        self.pagingEnabled = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator   = NO;
        
    }
    return self;
}


-(void)addItemData:(NSArray *)itemData andImageData:(NSArray *)imageArray;{
    _itemData   = itemData;
    _imageArray = imageArray;
    
    [self configView];
}


-(void)configView{
    
    CGFloat btnW = self.frame.size.width/5;
    CGFloat btnH = self.frame.size.width/5;
    for (NSInteger i = 0; i< self.itemData.count; i++) {
        
        self.typeBtn         = [UIButton createCustomButtonWithFrame:CGRectMake(i*btnW, 0, btnW, btnH) title:self.itemData[i] backGroungColor:[UIColor whiteColor] titleColor:WYCOLOR(121, 121, 121, 1.0) font:[UIFont wyNormalFot:13]];
        
        [self.typeBtn setImage:[UIImage imageNamed:self.imageArray[i]] forState:UIControlStateNormal];
        
        [self.typeBtn setTitleColor:WYCOLOR(87, 87, 87, 1.0) forState:UIControlStateSelected];//WYCOLOR(255, 237, 167, 1.0)
        
        self.typeBtn.tag     = i + BTNTag;
        
        if (i == 0 ) {
            self.typeBtn.selected = YES;
            self.typeBtn.titleLabel.font = [UIFont wyBlodFont:15];
            _oldBtn = self.typeBtn;
        }
        
        [self.typeBtn layoutButtonWithEdgeInsetsStyle:WYButtonEdgeInsetsStyleTop imageTitleSpace:8];
        [self.typeBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.typeBtn];
    }
    self.contentSize = CGSizeMake(btnW*self.itemData.count,btnH);
    
}


-(void)btnClick:(UIButton *)sender{
    
    self.block(sender.titleLabel.text,sender.tag - BTNTag);
    _oldBtn.titleLabel.font = [UIFont wyNormalFot:13];
    sender.titleLabel.font  = [UIFont wyBlodFont:15];
    
    if (_oldBtn != sender) {
        _oldBtn.selected = !_oldBtn.selected;
        sender.selected = !sender.selected;
    }
    _oldBtn = sender;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
